package com.nbjtjc.safe.service.cost;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostBudgetItemMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetMonthMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetPlanMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetPlanTempletMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetSubItemMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.cost.CostAccountReg;
import com.nbjtjc.safe.model.cost.CostBudgetItem;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.cost.CostBudgetMonth;

import com.nbjtjc.safe.model.cost.CostBudgetSubItem;
import com.nbjtjc.safe.model.know.SafeKnowLabel;
import com.nbjtjc.safe.model.know.SafeKnowledge;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.FileUtil;
import com.nbjtjc.safe.util.MyShiroUtils;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.collection.CollectionUtil;

@Service
public class CostBudgetPlanService {
	@Autowired
	  private CostBudgetPlanMapper costBudgetPlanMapper;
	@Autowired
	  private CostBudgetItemMapper costBudgetItemMapper;
	@Autowired
	  private CostBudgetSubItemMapper costBudgetSubItemMapper;
	@Autowired
	  private CostBudgetMonthMapper costBudgetMonthMapper;
    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private SysDeptMapper sysDeptMapper;
//    @Autowired
//    private CostBudgetPlanTempletMapper costBudgetPlanTempletMapper;
	
	  //不是按模板生成项目，增加月度预算，旧的新增和更新接口，接口1
//    @Transactional(rollbackFor = {Throwable.class})
//	  public Result add(CostBudgetPlan costBudgetPlan){
//		        Result result=new Result();
//		        if(ObjectUtil.isNull(costBudgetPlan)
//		        		){		            
//		        	result.setFalse("新增或修改内容不能为空");
//		            return result;
//		        }
//		        
//		        //主键为空或库中无此主键则为新建，插入数据
//		        if(ObjectUtil.isNull(costBudgetPlan.getId())|| ObjectUtil.isNull(costBudgetPlanMapper.selectByPrimaryKey(costBudgetPlan.getId()))){
//			        //同样公司年份一直能有一个
//			        if(ObjectUtil.isNotNull(costBudgetPlan.getYear())){
//			        	CostBudgetPlan costBudgetPlanYear=new CostBudgetPlan();
//			        	costBudgetPlanYear.setYear(costBudgetPlan.getYear());
//			        	costBudgetPlanYear.setDeptId(MyShiroUtils.getCompanyId());
//			        	if(CollectionUtil.isNotEmpty(costBudgetPlanMapper.search(costBudgetPlanYear))){
//				        	result.setFalse("该年份已有预算计划，不能重复增加");
//				            return result;
//			        	}
//			        	
//			        }else{
//			        	result.setFalse("年份不能为空");
//			            return result;
//			        }
//		        	
//		        	int res=costBudgetPlanMapper.insertSelective(costBudgetPlan);
//			        if(res==0){
//			            result.setFalse("新建预算计划失败");
//			            return result;
//			        }
//			        
//
//			        
//			        //插入项目内容
//			       //项目内容不为空则插入，为空跳过无动作
//		 	    	if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
//				        result = insertCostBudgetItems(costBudgetPlan);
//
//				        if(result.isSuccess()==false){
//				            return result;
//				        }
//		 	    	}
//		        }else
//		        {
//		        	int res=costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
//		        	 if(res==0){
//		        		 result.setFalse("更新预算计划失败");
//				         return result;
//
//		        	 }
//		        	 //更新项目内容
//		        	 //项目内容不为空则更新，为空跳过无动作
//		 	    	if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
//		 	    		result= updateCostBudgetItems(costBudgetPlan);
//				        if(result.isSuccess()==false){
//				            return result;
//				        }
//				    }
//		        }
//		        result.setData(costBudgetPlan);
//
//		        return result;
//		    }
    
	  //不是按模板生成项目，增加月度预算，新的新增和更新接口，接口3
    @Transactional(rollbackFor = {Throwable.class})
  public Result add3(CostBudgetPlan costBudgetPlan){
        Result result=new Result();
        if(ObjectUtil.isNull(costBudgetPlan))
        		{		            
        	result.setFalse("预算计划内容不能为空");
            return result;
        }
        
        if(ObjectUtil.isNull(costBudgetPlan.getCostBudgetItems())){
        	result.setFalse("预算计划项目不能为空");
            return result;
        }
        
        if(ObjectUtil.isNull(costBudgetPlan.getId())|| 
        		ObjectUtil.isNull(costBudgetPlanMapper.selectByPrimaryKey(costBudgetPlan.getId()))){//新增预算计划
	        //年份验证，同一个公司下的计划不可为空，不可重复
        	if(ObjectUtil.isNotNull(costBudgetPlan.getYear())){
	        	CostBudgetPlan costBudgetPlanYear=new CostBudgetPlan();
	        	costBudgetPlanYear.setYear(costBudgetPlan.getYear());
	        	costBudgetPlanYear.setDeptId(MyShiroUtils.getCompanyId());
	        	
	        	
	        	if(CollectionUtil.isNotEmpty(costBudgetPlanMapper.search(costBudgetPlanYear))){
		        	result.setFalse("该年份已有预算计划，不能重复增加");
		            return result;
	        	}
	        	
	        }else{
	        	result.setFalse("年份不能为空");
	            return result;
	        }
        	//记录当前用户单位作为预算计划公司
        	int  companeyId= MyShiroUtils.getCompanyId();
        	costBudgetPlan.setDeptId(companeyId);
        	costBudgetPlan.setDeptName(sysDeptMapper.findNameById(companeyId));
        	costBudgetPlan.setCreateDate(new Date());
        	costBudgetPlan.setCreateId(MyShiroUtils.getSessionUserId());
	        
        	//新建预算计划
        	int res=costBudgetPlanMapper.insertSelective(costBudgetPlan);
	        if(res==0){
	            result.setFalse("新建预算计划失败");
	            return result;
	        }
	        
	        //获取新建预算计划的ID，以此创建后续项目、二级项目、月度预算
	        if(ObjectUtil.isNull(costBudgetPlan.getId())|| costBudgetPlan.getId() == 0){
	            result.setFalse("获取新建预算计划的ID失败");
	            return result;
	        }
	        int planId = costBudgetPlan.getId();
	        //创建预算计划主表的总月度预算
	        CostBudgetMonth costBudgetMonth = new CostBudgetMonth();
	        costBudgetMonth.setItemType(3);
	        costBudgetMonth.setPlanId(planId);
	        costBudgetMonth.setYear(costBudgetPlan.getYear());
	        res = costBudgetMonthMapper.insertSelective(costBudgetMonth);
	        if(res==0){
	            result.setFalse("创建预算计划月度预算失败");
	            return result;
	        }
	        //创建项目
	        int  costBudgetItemsSize=costBudgetPlan.getCostBudgetItems().size();
	        //遍历创建一级项目
	        for(int i=0;i<costBudgetItemsSize;i++){
	        	 //创建一级项目内容
	        	CostBudgetItem costBudgetItemIndex= costBudgetPlan.getCostBudgetItems().get(i);
	        	//一级项目对象和名称不能为空，为空则报错
	        	if(ObjectUtil.isNull(costBudgetItemIndex)||ObjectUtil.isNull(costBudgetItemIndex.getItem())){
		            result.setFalse("一级项目名称不能为空");
		            return result;
	        	}else if(CollectionUtil.isEmpty(costBudgetItemIndex.getCostBudgetSubItems())){
		            result.setFalse("二级项目子项目不能为空");
		            return result;
        		}else{
	        		
		        //不为空创建一级项目
		        	//先设置budget_plan_id、create_date、create_id
	        		costBudgetItemIndex.setBudgetPlanId(planId);
	        		costBudgetItemIndex.setCreateDate(new Date());
	        		costBudgetItemIndex.setCreateId(MyShiroUtils.getSessionUserId());
	        		
	        		//创建一级项目
			        res = costBudgetItemMapper.insertSelective(costBudgetItemIndex);
			        if(res==0){
			            result.setFalse("创建预算计划一级项目失败");
			            return result;
			        }
			        
			        
			        int itemId = costBudgetItemIndex.getId();
			        //遍历创建二级子项目
			        int subItemsSize = costBudgetItemIndex.getCostBudgetSubItems().size();
			        for(int j=0; j < subItemsSize;j++){
			        	CostBudgetSubItem costBudgetSubItemIndex = costBudgetItemIndex.getCostBudgetSubItems().get(i);
			        	//子项目名称不能为空
			        	if(ObjectUtil.isNull(costBudgetSubItemIndex)||ObjectUtil.isNull(costBudgetSubItemIndex.getItem())){
				            result.setFalse("二级项目子项目名称不能为空");
				            return result;
			        	}else{
			        	//不为空创建子项目
			        		//先设置budget_item_id、create_date、create_id
			        		costBudgetSubItemIndex.setBudgetItemId(itemId);
			        		costBudgetSubItemIndex.setCreateDate(new Date());
			        		costBudgetSubItemIndex.setCreateId(MyShiroUtils.getSessionUserId());
			        		
			    	        //创建子项目内容
			    	        res = costBudgetSubItemMapper.insertSelective(costBudgetSubItemIndex);
			    	        if(res==0){
			    	            result.setFalse("创建预算计划二级项目失败");
			    	            return result;
			    	        }
			    	        
			    	        costBudgetItemIndex.getCostBudgetSubItems().set(j,costBudgetSubItemIndex);

			        	}
			        }
			        costBudgetPlan.getCostBudgetItems().set(i, costBudgetItemIndex);

			        
	        		
	        	}
	        }
	       
	        //创建一级项目月度预算
	        res = costBudgetItemMapper.insertItemMonthTemplet(costBudgetPlan);
	        if(res==0){
	            result.setFalse("创建预算计划一级项目月度预算失败");
	            return result;
	        }
	        

	        //创建子项目月度预算
	        res = costBudgetSubItemMapper.insertSubItemMonthTemplet(costBudgetPlan);
	        if(res==0){
	            result.setFalse("创建预算计划二级子项目月度预算失败");
	            return result;
	        }
	        

	       
	        
        }else{//更新预算计划
//        	//计算计划预算总额
//    		float total = (float) 0;
//    		if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
//	        	for(CostBudgetItem costBudgetItem:costBudgetPlan.getCostBudgetItems()){
//        			if(ObjectUtil.isNotNull(costBudgetItem.getId())&&ObjectUtil.isNotNull(costBudgetItem.getItemTotalBudget())){
//        				total += costBudgetItem.getItemTotalBudget();
//        			}
//        		}
//    		}
//        	costBudgetPlan.setTotalBudget(total);
        	
        	 //根据二级项目下的各月预算金额计算各级项目预算金额和预算计划总金额
        	 result = calculateBudgetYear(costBudgetPlan);
        	 if(result.isSuccess()){
	        	 costBudgetPlan = 	(CostBudgetPlan) result.getData();
        	 }else{
        		 return result;
        	 }
        	 costBudgetPlan.setModifyDate(new Date());
        	 costBudgetPlan.setModifyId(MyShiroUtils.getSessionUserId());
        	 
        	int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
        	 if(res==0){
        		 result.setFalse("更新预算计划失败");
		         return result;
        	 }
        	 
        	 //更新子对象
        	 result = updatePlanAllItems(costBudgetPlan);
        	 if(result.isSuccess()==false){
        		 return result;
        	 }
        	 
        	 //计算各级预算月度金额
        	 res=-1;
        	 res = updateMonth(costBudgetPlan.getId());
	       	 if(res==0){
	    		 result.setFalse("更新各级预算月度金额失败");
		         return result;
	    	 }
        }      
        result.setData(costBudgetPlan);

	    return result;
  }

	    private Result insertCostBudgetItems(CostBudgetPlan costBudgetPlan){
	        Result result=new Result();

	    	//将CostBudgetItems的budgetPlanId都设为当前costBudgetPlan的ID
	    	List<CostBudgetItem> costBudgetItems =  costBudgetPlan.getCostBudgetItems();
			for(CostBudgetItem costBudgetItem: costBudgetItems){
				costBudgetItem.setBudgetPlanId(costBudgetPlan.getId());
				costBudgetItem.setCancel(0);
			}
			
	    	int res=costBudgetItemMapper.insertList(costBudgetItems);
	    	if(res==0){
	            result.setFalse("插入费用预算项目失败");
	            return result;
	        }
	    	result.setData(costBudgetItems);
	    	return result;
	    }
	    
	    private Result updateCostBudgetItems(CostBudgetPlan costBudgetPlan){
	        Result result=new Result();
	    	if(CollectionUtil.isEmpty(costBudgetPlan.getCostBudgetItems())){
	            result.setFalse("更新对象为空");
	        	return result;
		    }
	    	ArrayList<Integer> costBudgetItemsIdDataBase = costBudgetItemMapper.findIdByPlanId(costBudgetPlan.getId());
	    	ArrayList<Integer> costBudgetItemsId = new ArrayList();
	    	//将CostBudgetItems的budgetPlanId都设为当前costBudgetPlan的ID
	    	//若库中已有该项目（ID不为空，库中查得到）则更新，否则（该项目ID为空，或库中查不到）插入
	    	List<CostBudgetItem> costBudgetItems =  costBudgetPlan.getCostBudgetItems();
	    	
			for(CostBudgetItem costBudgetItem: costBudgetItems){
				//项目为空则跳过，继续下一个
				if(ObjectUtil.isNull(costBudgetItem)){
		            continue;
				}
				//如果ID不为空，加入costBudgetItemsId
				if(ObjectUtil.isNotNull(costBudgetItem.getId())){
					costBudgetItemsId.add(costBudgetItem.getId());
				}
				int res=0;
		    	//将CostBudgetItem的budgetPlanId设为当前costBudgetPlan的ID
				costBudgetItem.setBudgetPlanId(costBudgetPlan.getId());
				//库中已有该项目（ID不为空，库中查得到）则更新
				if(ObjectUtil.isNotNull(costBudgetItem.getId()) && ObjectUtil.isNotNull(costBudgetItemMapper.selectByPrimaryKey(costBudgetItem.getId()))){
					res = costBudgetItemMapper.updateByPrimaryKeySelective(costBudgetItem);
			    	if(res==0){
			            result.setFalse("更新费用预算项目失败");
			            return result;
			        }
				}else{
					//（该项目ID为空，或库中查不到）插入
			    	res=costBudgetItemMapper.insertSelective(costBudgetItem);
			    	if(res==0){
			            result.setFalse("插入费用预算项目失败");
			            return result;
			        }
				}
			}
			//库中有而更新内容没有的项目的ID，是要删除的项目
			for(Integer id:costBudgetItemsIdDataBase){
				int res=-1;
				if(costBudgetItemsId.contains(id)==false){
					res = costBudgetItemMapper.deleteByPrimaryKey(id);
			    	if(res==0){
			            result.setFalse("删除库中费用预算项目失败");
			            return result;
			        }
				}

			}
	    	result.setData(costBudgetItems);
	    	return result;
	    }
	  
	  public Result delete(int id){
	      Result result=new Result();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = costBudgetPlanMapper.deleteByPrimaryKey(id);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	      }
	      return result;
	  }
	  
	  public Result delete(CostBudgetPlan costBudgetPlan){
	      Result result=new Result();
	      if(ObjectUtil.isNull(costBudgetPlan.getId())){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      costBudgetPlan.setCancel(1);
	      costBudgetPlan.setModifyDate(new Date());
	      costBudgetPlan.setModifyId(MyShiroUtils.getSessionUserId());
	      int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	      }
	      List<CostBudgetItem> costBudgetItems= costBudgetItemMapper.findDetailByPlanId(costBudgetPlan.getId());
	      for(CostBudgetItem costBudgetItem:costBudgetItems){
	    	  costBudgetItem.setCancel(1);
	    	  costBudgetItemMapper.updateByPrimaryKeySelective(costBudgetItem);
		      costBudgetSubItemMapper.cancelByItemId(costBudgetItem.getId());
	      }
	      return result;
	  }
	  
	  public Result find(CostBudgetPlan costBudgetPlan) {
	      Result result = new Result();
	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||costBudgetPlan.getDeptId()<=0){
	    	  costBudgetPlan.setDeptId(MyShiroUtils.getCompanyId());
	      }
	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.search(costBudgetPlan);
	      result.setData(new PageInfo<>(costBudgetPlans));

	      return result;
	  }
	  
	  public Result findDetail(CostBudgetPlan costBudgetPlan) {
	      Result result = new Result();
	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||costBudgetPlan.getDeptId()<=0){
	    	  costBudgetPlan.setDeptId(MyShiroUtils.getCompanyId());
	      }
	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
	      result.setData(new PageInfo<>(costBudgetPlans));

	      return result;
	  }
	  
	  public Result findSubcompany(CostBudgetPlan costBudgetPlan) {
	      Result result = new Result();
	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
	        	result.setFalse("缺少公司ID或年份,无法据此查看下级公司预算计划");
	        	return result;
	      }
	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchMonth(costBudgetPlan);
	      costBudgetPlans.addAll(costBudgetPlanMapper.searchSubcompany(costBudgetPlan.getDeptId(),costBudgetPlan.getYear()));
	      result.setData(new PageInfo<>(costBudgetPlans));

	      return result;
	  }
	//0907-1 未递归汇总，顾舍弃 
//	  public Result findSubcompanyItems(CostBudgetPlan costBudgetPlan) {
//	      Result result = new Result();
//	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
//	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
//	        	result.setFalse("缺少公司ID或年份,无法据此查看下级公司预算计划");
//	        	return result;
//	      }
//	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchItems(costBudgetPlan);
//	      costBudgetPlans.addAll(costBudgetPlanMapper.searchSubcompanyItems(costBudgetPlan.getDeptId(),costBudgetPlan.getYear()));
//	      result.setData(new PageInfo<>(costBudgetPlans));
//
//	      return result;
//	  }
	  
	  public Result findSubcompanyItems(CostBudgetPlan costBudgetPlan) {
	      Result result = new Result();
	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
	        	result.setFalse("缺少公司ID或年份,无法据此查看下级公司预算计划");
	        	return result;
	      }
	      List<CostBudgetPlan> costBudgetPlans = findSubcompanyItemsRecursion(costBudgetPlan);
	      
	      result.setData(new PageInfo<>(costBudgetPlans));

	      return result;
	  }
	  
	  //0907-1 递归汇总下级单位预算
	  public List<CostBudgetPlan> findSubcompanyItemsRecursion(CostBudgetPlan costBudgetPlan) {
		  if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
	        	return null;
	      }
	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchItems(costBudgetPlan);
	      if(CollectionUtil.isEmpty(costBudgetPlans)||ObjectUtil.isNull(costBudgetPlans.get(0))){
	        	return null;
	      }
	      List<CostBudgetPlan> costBudgetPlanSubComps = costBudgetPlanMapper.searchSubcompanyItems(costBudgetPlan.getDeptId(),costBudgetPlan.getYear());
	      	      
	      if(CollectionUtil.isNotEmpty(costBudgetPlanSubComps)){
	    	  for(int i = 0;i<costBudgetPlanSubComps.size();i++){
	    		  if(ObjectUtil.isNull(costBudgetPlanSubComps.get(i))){
	    			  continue;
	    		  }
	    		  CostBudgetPlan costBudgetPlan1 = findSubcompanyItemsRecursion(costBudgetPlanSubComps.get(i)).get(0);
		    	  costBudgetPlanSubComps.set(i, costBudgetPlan1);
	    	  }
		      costBudgetPlans.addAll(costBudgetPlanSubComps);
	      }
	      if(CollectionUtil.isNotEmpty(costBudgetPlanSubComps)){
	    	  if(ObjectUtil.isNull(costBudgetPlans.get(0).getTotalBudget())){
	    		  costBudgetPlans.get(0).setTotalBudget((float) 0);
	    	  }
	    	  float planBudget = (float) 0;
	    	  for(int i = 0;i<costBudgetPlans.size();i++){
	    		  if(ObjectUtil.isNotNull(costBudgetPlans.get(i))&&ObjectUtil.isNotNull(costBudgetPlans.get(i).getTotalBudget())){
	    			  planBudget =+ costBudgetPlans.get(i).getTotalBudget();
	    		  }
	    	  }
	    	  costBudgetPlans.get(0).setTotalBudget(planBudget);
	    	  
	    	  for(int j=0;j<9;j++){
//		    	  CostBudgetItem costBudgetItemTemp=new CostBudgetItem();
//		    	  costBudgetItemTemp.setItemTotalBudget((float)0);
	    		  float itemTotalBudget= 0;
			      for(int i = 0;i<costBudgetPlans.size();i++){
			    	  if(ObjectUtil.isNotNull(costBudgetPlans.get(i))
			    			  &&CollectionUtil.isNotEmpty(costBudgetPlans.get(i).getCostBudgetItems())
			    			  &&ObjectUtil.isNotNull(costBudgetPlans.get(i).getCostBudgetItems().get(j))
			    			  &&ObjectUtil.isNotNull(costBudgetPlans.get(i).getCostBudgetItems().get(j).getItemTotalBudget())
			    			  ){
			    		  itemTotalBudget=+costBudgetPlans.get(i).getCostBudgetItems().get(j).getItemTotalBudget();
			    	  }
			      }
		    	  costBudgetPlans.get(0).getCostBudgetItems().get(j).setItemTotalBudget(itemTotalBudget);;
	    	  }
	      }

	      return costBudgetPlans;
	  }
	  //0908-1未递归顾舍弃
//	  public Result findTrackSubcompany(CostBudgetPlan costBudgetPlan) {
//	      Result result = new Result();
////	      PageHelper.startPage(costBudgetPlan.getPageCurrent(), costBudgetPlan.getPageSize());
//	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
//	        	result.setFalse("缺少公司ID或年份,无法据此查看下级公司预算计划");
//	        	return result;
//	      }
//	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
//	      costBudgetPlans.addAll(costBudgetPlanMapper.searchTrackSubcompany(costBudgetPlan.getDeptId(),costBudgetPlan.getYear()));
//	      //检查每个计划的总投入，为空则赋0
//	      if(CollectionUtil.isNotEmpty(costBudgetPlans)){
//		      for(int i =0 ;i<costBudgetPlans.size();i++){
//		    	  if(CollectionUtil.isEmpty(costBudgetPlans.get(i).getCostAccountRegs())){
//		    		  CostAccountReg costAccountRegT = new CostAccountReg();
//		    		  costAccountRegT.setTotalAccount((float)0);
//		    		  List<CostAccountReg> costAccountRegsT = new ArrayList();
//		    		  costAccountRegsT.add(costAccountRegT);
//		    		  costBudgetPlans.get(i).setCostAccountRegs(costAccountRegsT);
//		    	  }else{
//		    		  if(ObjectUtil.isNull(costBudgetPlans.get(i).getCostAccountRegs().get(0).getTotalAccount())){
//		    			  costBudgetPlans.get(i).getCostAccountRegs().get(0).setTotalAccount((float) 0);
//		    		  }
//		    	  }
//		      }
//	      }
//	      result.setData(costBudgetPlans);
//
//	      return result;
//	  }
	  //0908-1 递归计算下级单位汇总预算跟踪接口
	  public Result findTrackSubcompany(CostBudgetPlan costBudgetPlan) {
      Result result = new Result();
      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
        	result.setFalse("缺少公司ID或年份,无法据此查看下级公司预算计划");
        	return result;
      }
      
      List<CostBudgetPlan> costBudgetPlans = findTrackSubcompanyRecursion(costBudgetPlan);

      result.setData(costBudgetPlans);

      return result;
	  }
	  
	  //0908-1 递归汇总下级单位预算跟踪方法
	  public List<CostBudgetPlan> findTrackSubcompanyRecursion(CostBudgetPlan costBudgetPlan) {
		  if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||ObjectUtil.isNull(costBudgetPlan.getYear()) ||costBudgetPlan.getDeptId()==-1){
	        	return null;
	      }
	      List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
	      if(CollectionUtil.isEmpty(costBudgetPlans)||ObjectUtil.isNull(costBudgetPlans.get(0))){
	        	return null;
	      }
	      List<CostBudgetPlan> costBudgetPlanSubComps = costBudgetPlanMapper.searchTrackSubcompany(costBudgetPlan.getDeptId(),costBudgetPlan.getYear());
	      
	      //不为空的下级单位预算进行递归
	      if(CollectionUtil.isNotEmpty(costBudgetPlanSubComps)){
	    	  for(int i = 0;i<costBudgetPlanSubComps.size();i++){
	    		  if(ObjectUtil.isNull(costBudgetPlanSubComps.get(i))){
	    			  continue;
	    		  }
	    		  CostBudgetPlan costBudgetPlan1 = findTrackSubcompanyRecursion(costBudgetPlanSubComps.get(i)).get(0);
		    	  costBudgetPlanSubComps.set(i, costBudgetPlan1);
	    	  }
		      costBudgetPlans.addAll(costBudgetPlanSubComps);
	      }
	      
	      if(CollectionUtil.isNotEmpty(costBudgetPlanSubComps)){
	    	  //预算如果为空则设为0
	    	  if(ObjectUtil.isNull(costBudgetPlans.get(0).getTotalBudget())){
	    		  costBudgetPlans.get(0).setTotalBudget((float) 0);
	    	  }
	    	  float planBudget = (float) 0;
	    	  float costAccount = (float) 0;
	    	  
	    	  for(int i = 0;i<costBudgetPlans.size();i++){
	    		  if(ObjectUtil.isNotNull(costBudgetPlans.get(i))){
	    			  //汇总预算
	    			  if(ObjectUtil.isNotNull(costBudgetPlans.get(i).getTotalBudget())){
		    			  planBudget =+ costBudgetPlans.get(i).getTotalBudget();
	    			  }
	    			  
	    			  //检查每个计划的总投入，为空则赋0
	    	    	  if(CollectionUtil.isEmpty(costBudgetPlans.get(i).getCostAccountRegs())){
	    	    		  CostAccountReg costAccountRegT = new CostAccountReg();
	    	    		  costAccountRegT.setTotalAccount((float)0);
	    	    		  List<CostAccountReg> costAccountRegsT = new ArrayList();
	    	    		  costAccountRegsT.add(costAccountRegT);
	    	    		  costBudgetPlans.get(i).setCostAccountRegs(costAccountRegsT);
	    	    	  }else{
	    	    		  if(ObjectUtil.isNull(costBudgetPlans.get(i).getCostAccountRegs().get(0).getTotalAccount())){
	    	    			  costBudgetPlans.get(i).getCostAccountRegs().get(0).setTotalAccount((float) 0);
	    	    		  }else{
	    	    			  costAccount=+costBudgetPlans.get(i).getCostAccountRegs().get(0).getTotalAccount();
	    	    		  }
	    	    	  }
	    		  }
	    	  }
	    	  
	    	  costBudgetPlans.get(0).setTotalBudget(planBudget);
	    	  costBudgetPlans.get(0).getCostAccountRegs().get(0).setTotalAccount(costAccount);
	    	  
	      }

	      return costBudgetPlans;
	  }
	  
	  public Result searchByYear(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();
	        Integer planId = 0;
	        
	        	if(ObjectUtil.isNull(costBudgetPlan)||ObjectUtil.isNull(costBudgetPlan.getYear())){
		        	result.setFalse("年份不能为空");
		        	return result;
	        	}
	  	      if(ObjectUtil.isNull(costBudgetPlan.getDeptId())||costBudgetPlan.getDeptId()<=0){
		    	  costBudgetPlan.setDeptId(MyShiroUtils.getCompanyId());
		      }
	        	List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
	        	if(CollectionUtil.isEmpty(costBudgetPlans)){
		        	result.setFalse("无此年份的预算计划");
		        	return result;
	        	}else if(costBudgetPlans.size()>1){
		        	result.setFalse("此年份的预算计划有多个，请检查");
		        	return result;
	        	}
	        	
	        	List<CostAccountReg> accountRegs= costBudgetPlans.get(0).getCostAccountRegs();
	        	
	        	if(CollectionUtil.isEmpty(accountRegs)){
	        		accountRegs.add(new CostAccountReg() );
	        	}else if(accountRegs.size()>1){
		        	result.setFalse("此预算计划对应的费用投入有多个，请检查");
		        	return result;
	        	}
	        	if(ObjectUtil.isNull(costBudgetPlans.get(0).getTotalBudget())){
	        		costBudgetPlans.get(0).setTotalBudget((float)0);;
	        	}
	        	if(ObjectUtil.isNull(accountRegs.get(0).getTotalAccount())){
	        		accountRegs.get(0).setTotalAccount((float)0);
	        	}
	        	result.setData(costBudgetPlans.get(0));

	        return result;
	    }
	  
	  public Result show(int id) {
	      Result result = new Result();
	      CostBudgetPlan costBudgetPlan = costBudgetPlanMapper.show(id);
	      result.setData(costBudgetPlan);

	      return result;
	  }
	  
	  //按模板生成项目，增加月度预算后的计划新增和更新接口，接口2
	    @Transactional(rollbackFor = {Throwable.class})
	  public Result addByTemplet(CostBudgetPlan costBudgetPlan){
	        Result result=new Result();
	        if(ObjectUtil.isNull(costBudgetPlan))
	        		{		            
	        	result.setFalse("预算计划内容不能为空");
	            return result;
	        }
	        
	        if(ObjectUtil.isNull(costBudgetPlan.getId())|| 
	        		ObjectUtil.isNull(costBudgetPlanMapper.selectByPrimaryKey(costBudgetPlan.getId()))){//新增预算计划
		        //年份验证，同一个公司下的计划不可为空，不可重复
	        	if(ObjectUtil.isNotNull(costBudgetPlan.getYear())){
		        	CostBudgetPlan costBudgetPlanYear=new CostBudgetPlan();
		        	costBudgetPlanYear.setYear(costBudgetPlan.getYear());
		        	costBudgetPlanYear.setDeptId(MyShiroUtils.getCompanyId());
		        	
		        	
		        	if(CollectionUtil.isNotEmpty(costBudgetPlanMapper.search(costBudgetPlanYear))){
			        	result.setFalse("该年份已有预算计划，不能重复增加");
			            return result;
		        	}
		        	
		        }else{
		        	result.setFalse("年份不能为空");
		            return result;
		        }
	        	//记录当前用户单位作为预算计划公司
	        	int  companeyId= MyShiroUtils.getCompanyId();
	        	costBudgetPlan.setDeptId(companeyId);
	        	costBudgetPlan.setDeptName(sysDeptMapper.findNameById(companeyId));
	        	costBudgetPlan.setCreateDate(new Date());
	        	costBudgetPlan.setCreateId(MyShiroUtils.getSessionUserId());
		        
	        	//新建预算计划
	        	int res=costBudgetPlanMapper.insertSelective(costBudgetPlan);
		        if(res==0){
		            result.setFalse("新建预算计划失败");
		            return result;
		        }
		        
		        //获取新建预算计划的ID，以此创建后续项目、二级项目、月度预算
		        if(ObjectUtil.isNull(costBudgetPlan.getId())|| costBudgetPlan.getId() == 0){
		            result.setFalse("获取新建预算计划的ID失败");
		            return result;
		        }
		        int planId = costBudgetPlan.getId();
		        //创建预算计划月度预算
		        CostBudgetMonth costBudgetMonth = new CostBudgetMonth();
		        costBudgetMonth.setItemType(3);
		        costBudgetMonth.setPlanId(planId);
		        costBudgetMonth.setYear(costBudgetPlan.getYear());
		        res = costBudgetMonthMapper.insertSelective(costBudgetMonth);
		        if(res==0){
		            result.setFalse("创建预算计划月度预算失败");
		            return result;
		        }
		        
		        //创建一级项目内容
		        res = costBudgetItemMapper.insertItemByTemplet(costBudgetPlan);
		        if(res==0){
		            result.setFalse("创建预算计划一级项目失败");
		            return result;
		        }
		        //创建一级项目月度预算
		        res = costBudgetItemMapper.insertItemMonth(costBudgetPlan);
		        if(res==0){
		            result.setFalse("创建预算计划一级项目月度预算失败");
		            return result;
		        }
		        
		        //创建子项目内容
		        res = costBudgetSubItemMapper.insertSubItemByTemplet(costBudgetPlan);
		        if(res==0){
		            result.setFalse("创建预算计划二级项目失败");
		            return result;
		        }
		        //创建子项目月度预算
		        res = costBudgetSubItemMapper.insertSubItemMonth(costBudgetPlan);
		        if(res==0){
		            result.setFalse("创建预算计划二级子项目月度预算失败");
		            return result;
		        }
		        

		       
		        
	        }else{//更新预算计划
//	        	//计算计划预算总额
//        		float total = (float) 0;
//        		if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
//		        	for(CostBudgetItem costBudgetItem:costBudgetPlan.getCostBudgetItems()){
//	        			if(ObjectUtil.isNotNull(costBudgetItem.getId())&&ObjectUtil.isNotNull(costBudgetItem.getItemTotalBudget())){
//	        				total += costBudgetItem.getItemTotalBudget();
//	        			}
//	        		}
//        		}
//	        	costBudgetPlan.setTotalBudget(total);
	        	
	        	 //根据二级项目下的各月预算金额计算各级项目预算金额和预算计划总金额
	        	 result = calculateBudgetYear(costBudgetPlan);
	        	 if(result.isSuccess()){
		        	 costBudgetPlan = 	(CostBudgetPlan) result.getData();
	        	 }else{
	        		 return result;
	        	 }
	        	 costBudgetPlan.setModifyDate(new Date());
	        	 costBudgetPlan.setModifyId(MyShiroUtils.getSessionUserId());
	        	 
	        	int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
	        	 if(res==0){
	        		 result.setFalse("更新预算计划失败");
			         return result;
	        	 }
	        	 
	        	 //更新子对象
	        	 result = updatePlanAllItems(costBudgetPlan);
	        	 if(result.isSuccess()==false){
	        		 return result;
	        	 }
	        	 
	        	 //计算各级预算月度金额
	        	updateMonth(costBudgetPlan.getId());
	        }      
	        result.setData(costBudgetPlan);

		    return result;
	  }
	    //根据数据库中的子项目月度预算计算各级预算月度金额的方法
	    public int updateMonth(Integer planId) {
			// TODO Auto-generated method stub
	    	Result result= new Result();
	    	//更新该预算计划所有一级项目月度预算
		    int res1 = costBudgetMonthMapper.updateItemMonth(planId);
		    //更新该预算计划总的月度预算
	    	int res2 = costBudgetMonthMapper.updatePlanMonth(planId);
	    	return res1+res2;
		}

		//根据录入的二级项目月度预算计算二级项目全年总预算,一级项目全年总预算,预算计划全年总预算
		public Result calculateBudgetYear(CostBudgetPlan costBudgetPlan) {
			// TODO Auto-generated method stub
			Result result = new Result();
 			//计算一级项目预算
			//预算计划中一级项目是否为空
        	if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
        		//预算计划中的总预算先初始化为0
        		costBudgetPlan.setTotalBudget((float) 0);
        		
        		int i = 0;//0905-1增加
        		//遍历预算计划中一级项目数组
        		for(CostBudgetItem costBudgetItem:costBudgetPlan.getCostBudgetItems()){
        			//判断得到的一级项目需要有ID
        			if(ObjectUtil.isNotNull(costBudgetItem.getId())){
        				//如果cancel为1，即已被删除，不参与计算
        				if(ObjectUtil.isNotNull(costBudgetItem.getCancel())
        						&&costBudgetItem.getCancel()==1){
        					continue;
        				}
	        			//计算二级项目预算
        				//一级项目中的二级项目是否为空
	        			if(CollectionUtil.isNotEmpty(costBudgetItem.getCostBudgetSubItems())){
	        				//一级项目总预算先初始化为0
	        				costBudgetItem.setItemTotalBudget((float) 0);
	        				
	        				int j=0;
	        				//遍历二级项目数组
	        				for(CostBudgetSubItem costBudgetSubItem:costBudgetItem.getCostBudgetSubItems()){
	        					//判断得到的二级项目需要有ID
	        					if(ObjectUtil.isNotNull(costBudgetSubItem.getId())){
	        						//如果cancel为1，即已被删除，不参与计算
	        						if(ObjectUtil.isNotNull(costBudgetSubItem.getCancel())
	        								&&costBudgetSubItem.getCancel()==1){
	        							continue;
	        						}
		        		        	 //计算项目各月份预算
	        						//二级项目中的月度预算是否为空
		        					if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth())){
		        						//判断月度预算需要有ID
		        						if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth().getId())){
			        						//如果cancel为1，即已被删除，不参与计算
		        							if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth().getCancel())
		        									&&costBudgetSubItem.getCostBudgetMonth().getCancel()==1){
		        								continue;
		        							}
		        							//根据输入的月度数据计算12月之和,并赋给月度预算中的totalBudget,和二级项目的ItemBudget
		        							costBudgetSubItem.setItemBudget(costBudgetSubItem.getCostBudgetMonth().computeTotal());
		        							//将该二级项目预算加总到一级项目总预算中,遍历完二级项目后,该一级项目预算就计算完成
		        							costBudgetItem.setItemTotalBudget( costBudgetItem.getItemTotalBudget()+costBudgetSubItem.getItemBudget());
		        							
		        							//将二级项目更新到costBudgetItem中。 0905-1增加
		        							costBudgetItem.getCostBudgetSubItems().set(j, costBudgetSubItem);//0905-1增加
		        						}else{
			    	        				result.setData(costBudgetSubItem.getCostBudgetMonth());
			    	        				result.setFalse("待更新二级子项目的月度预算记录未传ID，更新失败");
			    	        				return result;
		        						}
		        					}else{
				        				result.setFalse("二级子项目的月度预算为空");
				        				return result;
		        					}
	        					}else{
	    	        				result.setData(costBudgetSubItem);
	    	        				result.setFalse("待更新二级项目未传ID，更新失败");
	    	        				return result;
	        					}
	        					j++;//0905-1增加
	        				}
	        				//将一级项目预算加总到预算计划总预算中
	        				costBudgetPlan.setTotalBudget(costBudgetPlan.getTotalBudget()+costBudgetItem.getItemTotalBudget());
	        				
	        				//将一级项目更新到costBudgetPlan中。 0905-1增加
	        				costBudgetPlan.getCostBudgetItems().set(i, costBudgetItem);//0905-1增加
	        			
	        			}else{
	        				//无二级子项目，则为零
	        				costBudgetItem.setItemTotalBudget((float) 0);
//	        				result.setFalse("二级子项目为空");
//	        				return result;
	        			}
        			}else{
        				
        				result.setData(costBudgetItem);
        				result.setFalse("待更新项目未传ID，更新失败");
        				return result;
        			}
        			i++;//0905-1增加
        		}
        	}else{
				result.setFalse("一级项目为空");
				return result;
        	}
	        result.setData(costBudgetPlan);
			return result;
		}
		
		  //只更新主表内容
	  public Result updatePlanMain(CostBudgetPlan costBudgetPlan){
	        Result result=new Result();
	        
	        if(ObjectUtil.isNull(costBudgetPlan)){
	        	result.setFalse("传入待更新预算计划为空");
	        	return result;
	        }else if(ObjectUtil.isNull(costBudgetPlan.getId())){
	        	result.setFalse("传入待更新预算计划Id为空");
	        	return result;
	        }else if(ObjectUtil.isNull(costBudgetPlan.getYear())){
	        	result.setFalse("传入待更新预算计划年份为空");
	        	return result;
	        }
        	int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
	       	if(res==0){
	       		result.setFalse("更新预算计划失败");
			    return result;
	       	}
	       	result.setData(costBudgetPlan);
	        return result;
	  }
		 //根据子项目月度预算更新预算计划子对象,包括一级项目,二级项目,二级项目月度预算
		public Result updatePlanAllItems(CostBudgetPlan costBudgetPlan){
			Result result = new Result();
			int res=-1;
 			//更新一级项目预算
        	if(CollectionUtil.isNotEmpty(costBudgetPlan.getCostBudgetItems())){
        		for(CostBudgetItem costBudgetItem:costBudgetPlan.getCostBudgetItems()){
        			if(ObjectUtil.isNotNull(costBudgetItem.getId()) ){
        				//如果cancel为1，即已被删除，不参与计算
        				if(ObjectUtil.isNotNull(costBudgetItem.getCancel())&&costBudgetItem.getCancel()==1
    					){
        					continue;
        				}
        				res=costBudgetItemMapper.updateByPrimaryKeySelective(costBudgetItem);
       	        	 if(res==0){
    	        		 result.setFalse("更新预算计划一级项目失败");
    			         return result;
    	        	 }
        			}else{
        				result.setData(costBudgetItem);
        				result.setFalse("待更新项目未传ID，更新失败");
        				return result;
        			}
        			
        			//更新二级项目预算
        			
        			if(CollectionUtil.isNotEmpty(costBudgetItem.getCostBudgetSubItems())){
        				for(CostBudgetSubItem costBudgetSubItem:costBudgetItem.getCostBudgetSubItems()){
        					if(ObjectUtil.isNotNull(costBudgetSubItem.getId())
        							
        					   ){	
        						//如果cancel为1，即已被删除，不参与计算
        						if(ObjectUtil.isNotNull(costBudgetSubItem.getCancel())&&costBudgetSubItem.getCancel()==1
    									){
        							continue;
        						}
        						//更新二级预算
        						res = costBudgetSubItemMapper.updateByPrimaryKeySelective(costBudgetSubItem);
        			        	 if(res==0){
        			        		 result.setFalse("更新预算计划二级项目失败");
        					         return result;
        			        	 }
        					}else{
    	        				result.setData(costBudgetSubItem);
    	        				result.setFalse("待更新二级项目未传ID，更新失败");
    	        				return result;
        					}
        		        	 //更新项目各月份预算
        					if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth())){
        						if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth().getId())){
        							//如果cancel为1，即已被删除，不参与计算
        							if(ObjectUtil.isNotNull(costBudgetSubItem.getCostBudgetMonth().getCancel())
        									&&costBudgetSubItem.getCostBudgetMonth().getCancel()==1){
        								continue;
        							}
//        							CostBudgetMonth costBudgetMonthTemp = costBudgetSubItem.getCostBudgetMonth();
        							res = costBudgetMonthMapper.updateByPrimaryKeySelective(costBudgetSubItem.getCostBudgetMonth());
	        			        	 if(res==0){
	        			        		 result.setFalse("更新预算计划二级项目的月度预算失败");
	        					         return result;
	        			        	 } 
        						}else{
	    	        				result.setData(costBudgetSubItem.getCostBudgetMonth());
	    	        				result.setFalse("待更新二级子项目的月度预算记录未传ID，更新失败");
	    	        				return result;
        						}
        					}
        				}
        			}
        		}
        	}
			
			return result;
		}
		
		//导入预算计划excel
	    public Result addBatch(MultipartFile multipartFile,CostBudgetPlan costBudgetPlan) throws IOException {
	        Result result=new Result();
	        
	        String fileName=multipartFile.getOriginalFilename();
	        File targetFile=new File(fileUtil.getFilePath()+multipartFile.getOriginalFilename());
	        if(!targetFile.getParentFile().exists()){
	            targetFile.getParentFile().mkdirs();
	        }
	        multipartFile.transferTo(targetFile);
	        if(fileName.endsWith("xls")){
	        	result =   readXls(targetFile,costBudgetPlan);
	        }else if(fileName.endsWith("xlsx")){
	        	result =  readXlsx(targetFile,costBudgetPlan);
	        }else {
	        	result.setFalse("传入文件不是excel表格，请检查");
         	return result;
	        }
	        return result;
	    }
	    //导入xls
	    public Result readXls(File excelFile,CostBudgetPlan costBudgetPlan) throws IOException {
	    	Result result=new Result();
	        InputStream is = new FileInputStream(excelFile);
	        // HSSFWorkbook 标识整个excel
	        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

	        //确认传入的costBudgetPlan有id值
	        if(ObjectUtil.isNull(costBudgetPlan)||ObjectUtil.isNull(costBudgetPlan.getId())){
	        	result.setFalse("预算计划不能为空,预算的ID不能为空");
	        	return result;
	        }
	        
        	costBudgetPlan.setDeptName(sysDeptMapper.findNameById(MyShiroUtils.getCompanyId()));
	        List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
	        if(ObjectUtil.isNull(costBudgetPlans)){
	        	result.setFalse("按数据库中查无此预算");
	        	return result;
	        }else if(costBudgetPlans.size()>1){
	        	result.setFalse("按数据库中查出预算数量不唯一");
	        	return result;
	        }else{
	        	costBudgetPlan=costBudgetPlans.get(0);
	        }


	        
	        List<CostBudgetMonth> costBudgetMonths = new ArrayList();
//	        CostBudgetMonth costBudgetMonthTemp = new CostBudgetMonth();

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
            //获取excel表中数据
            for (int rowNum = 3; rowNum < 35; rowNum++) {
            	HSSFRow hssfRow = hssfSheet.getRow(rowNum);
            	costBudgetMonths.add(new CostBudgetMonth());
            	costBudgetMonths.get(rowNum-3).setJan((float) (ObjectUtil.isNotNull(hssfRow.getCell(4).getNumericCellValue())?hssfRow.getCell(4).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setFeb((float) (ObjectUtil.isNotNull(hssfRow.getCell(5).getNumericCellValue())?hssfRow.getCell(5).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setMar((float) (ObjectUtil.isNotNull(hssfRow.getCell(6).getNumericCellValue())?hssfRow.getCell(6).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setApr((float) (ObjectUtil.isNotNull(hssfRow.getCell(7).getNumericCellValue())?hssfRow.getCell(7).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setMay((float) (ObjectUtil.isNotNull(hssfRow.getCell(8).getNumericCellValue())?hssfRow.getCell(8).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setJun((float) (ObjectUtil.isNotNull(hssfRow.getCell(9).getNumericCellValue())?hssfRow.getCell(9).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setJul((float) (ObjectUtil.isNotNull(hssfRow.getCell(10).getNumericCellValue())?hssfRow.getCell(10).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setAug((float) (ObjectUtil.isNotNull(hssfRow.getCell(11).getNumericCellValue())?hssfRow.getCell(11).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setSep((float) (ObjectUtil.isNotNull(hssfRow.getCell(12).getNumericCellValue())?hssfRow.getCell(12).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setOct((float) (ObjectUtil.isNotNull(hssfRow.getCell(13).getNumericCellValue())?hssfRow.getCell(13).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setNov((float) (ObjectUtil.isNotNull(hssfRow.getCell(14).getNumericCellValue())?hssfRow.getCell(14).getNumericCellValue():0));
            	costBudgetMonths.get(rowNum-3).setDece((float) (ObjectUtil.isNotNull(hssfRow.getCell(15).getNumericCellValue())?hssfRow.getCell(15).getNumericCellValue():0));
    	        costBudgetMonths.get(rowNum-3).setTotalBudget((float) (ObjectUtil.isNotNull(hssfRow.getCell(16).getNumericCellValue())?hssfRow.getCell(16).getNumericCellValue():0));
    	        costBudgetMonths.get(rowNum-3).setTotalPlanBudget((float) (ObjectUtil.isNotNull(hssfRow.getCell(17).getNumericCellValue())?hssfRow.getCell(17).getNumericCellValue():0));
//    	        costBudgetMonths.add(costBudgetMonthTemp);
            }
            
            int flag = 0;
			CostBudgetMonth costBudgetMonthTemp2 = new CostBudgetMonth();
			//将获取的表中数据赋给costBudgetPlan对象中子项目的月度预算.
            if(CollectionUtil.isEmpty(costBudgetPlan.getCostBudgetItems())){
	        	result.setFalse("该预算的所有一级项目为空,请查看");
	        	return result;
            }else{
                for(int i= 0;i<costBudgetPlan.getCostBudgetItems().size();i++){
                	if(CollectionUtil.isEmpty(costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems())){
        	        	result.setFalse("该预算的所有二级项目为空,请查看");
        	        	return result;
                	}else{
                    	for(int j = 0;j<costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().size();j++){
                    		if(ObjectUtil.isNull(costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).getCostBudgetMonth())){
                	        	result.setFalse("该预算有二级项目的无月度预算,请查看");
                	        	return result;
                    		}else{
                    			costBudgetMonthTemp2 = costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).getCostBudgetMonth();
                    			costBudgetMonthTemp2.setJan(costBudgetMonths.get(flag).getJan());
                    			costBudgetMonthTemp2.setFeb(costBudgetMonths.get(flag).getFeb());
                    			costBudgetMonthTemp2.setMar(costBudgetMonths.get(flag).getMar());
                    			costBudgetMonthTemp2.setApr(costBudgetMonths.get(flag).getApr());
                    			costBudgetMonthTemp2.setMay(costBudgetMonths.get(flag).getMay());
                    			costBudgetMonthTemp2.setJun(costBudgetMonths.get(flag).getJun());
                    			costBudgetMonthTemp2.setJul(costBudgetMonths.get(flag).getJul());
                    			costBudgetMonthTemp2.setAug(costBudgetMonths.get(flag).getAug());
                    			costBudgetMonthTemp2.setSep(costBudgetMonths.get(flag).getSep());
                    			costBudgetMonthTemp2.setOct(costBudgetMonths.get(flag).getOct());
                    			costBudgetMonthTemp2.setNov(costBudgetMonths.get(flag).getNov());
                    			costBudgetMonthTemp2.setDece(costBudgetMonths.get(flag).getDece());
                    			costBudgetMonthTemp2.setTotalBudget(costBudgetMonths.get(flag).getTotalBudget());
                    			costBudgetMonthTemp2.setTotalPlanBudget(costBudgetMonths.get(flag).getTotalPlanBudget());
                    			costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).setCostBudgetMonth(costBudgetMonthTemp2) ; 
                    			flag++;
                    		}
                    	}
                	}
                }
            }
//            if(flag!=32){
//	        	result.setFalse("预算项目总数不为32,与模板不一致,请查看");
//	        	return result;
//            }
            
	       	 //根据二级项目下的各月预算金额计算各级项目预算金额和预算计划总金额
	       	 result = calculateBudgetYear(costBudgetPlan);
	       	 if(result.isSuccess()){
		        	 costBudgetPlan = 	(CostBudgetPlan) result.getData();
	       	 }else{
	       		 return result;
	       	 }
	       	 
	       	int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
	       	 if(res==0){
	       		 result.setFalse("更新预算计划失败");
			         return result;
	       	 }
	       	 
	       	 //更新子对象
	       	 result = updatePlanAllItems(costBudgetPlan);
	       	 if(result.isSuccess()==false){
	       		 return result;
	       	 }
	       	 
	       	 //计算各级预算月度金额
	       	updateMonth(costBudgetPlan.getId());
       	
            
            
            result.setData(costBudgetPlan);

            return result;
	    }
	    
	    //导入xlsx
	    public  Result readXlsx(File excelFile,CostBudgetPlan costBudgetPlan) throws IOException {
	    	Result result=new Result();
	        InputStream is = new FileInputStream(excelFile);
	        // XSSFWorkbook 标识整个excel
	        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);

	        //确认传入的costBudgetPlan有id值
	        if(ObjectUtil.isNull(costBudgetPlan)||ObjectUtil.isNull(costBudgetPlan.getId())){
	        	result.setFalse("预算计划不能为空,预算的ID不能为空");
	        	return result;
	        }
	        
        	costBudgetPlan.setDeptName(sysDeptMapper.findNameById(MyShiroUtils.getCompanyId()));
	        List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.searchDetail(costBudgetPlan);
	        if(ObjectUtil.isNull(costBudgetPlans)){
	        	result.setFalse("按数据库中查无此预算");
	        	return result;
	        }else if(costBudgetPlans.size()>1){
	        	result.setFalse("按数据库中查出预算数量不唯一");
	        	return result;
	        }else{
	        	costBudgetPlan=costBudgetPlans.get(0);
	        }


	        
	        List<CostBudgetMonth> costBudgetMonths = new ArrayList();
	        CostBudgetMonth costBudgetMonthTemp = new CostBudgetMonth();

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
            //获取excel表中数据
            for (int rowNum = 4; rowNum <= 35; rowNum++) {
            	XSSFRow xssfRow = xssfSheet.getRow(rowNum);

    	        costBudgetMonthTemp.setJan((float) (ObjectUtil.isNotNull(xssfRow.getCell(4).getNumericCellValue())?xssfRow.getCell(4).getNumericCellValue():0));
    	        costBudgetMonthTemp.setFeb((float) (ObjectUtil.isNotNull(xssfRow.getCell(5).getNumericCellValue())?xssfRow.getCell(5).getNumericCellValue():0));
    	        costBudgetMonthTemp.setMar((float) (ObjectUtil.isNotNull(xssfRow.getCell(6).getNumericCellValue())?xssfRow.getCell(6).getNumericCellValue():0));
    	        costBudgetMonthTemp.setApr((float) (ObjectUtil.isNotNull(xssfRow.getCell(7).getNumericCellValue())?xssfRow.getCell(7).getNumericCellValue():0));
    	        costBudgetMonthTemp.setMay((float) (ObjectUtil.isNotNull(xssfRow.getCell(8).getNumericCellValue())?xssfRow.getCell(8).getNumericCellValue():0));
    	        costBudgetMonthTemp.setJun((float) (ObjectUtil.isNotNull(xssfRow.getCell(9).getNumericCellValue())?xssfRow.getCell(9).getNumericCellValue():0));
    	        costBudgetMonthTemp.setJul((float) (ObjectUtil.isNotNull(xssfRow.getCell(10).getNumericCellValue())?xssfRow.getCell(10).getNumericCellValue():0));
    	        costBudgetMonthTemp.setAug((float) (ObjectUtil.isNotNull(xssfRow.getCell(11).getNumericCellValue())?xssfRow.getCell(11).getNumericCellValue():0));
    	        costBudgetMonthTemp.setSep((float) (ObjectUtil.isNotNull(xssfRow.getCell(12).getNumericCellValue())?xssfRow.getCell(12).getNumericCellValue():0));
    	        costBudgetMonthTemp.setOct((float) (ObjectUtil.isNotNull(xssfRow.getCell(13).getNumericCellValue())?xssfRow.getCell(13).getNumericCellValue():0));
    	        costBudgetMonthTemp.setNov((float) (ObjectUtil.isNotNull(xssfRow.getCell(14).getNumericCellValue())?xssfRow.getCell(14).getNumericCellValue():0));
    	        costBudgetMonthTemp.setDece((float) (ObjectUtil.isNotNull(xssfRow.getCell(15).getNumericCellValue())?xssfRow.getCell(15).getNumericCellValue():0));
    	        costBudgetMonthTemp.setTotalBudget((float) (ObjectUtil.isNotNull(xssfRow.getCell(16).getNumericCellValue())?xssfRow.getCell(16).getNumericCellValue():0));
    	        costBudgetMonthTemp.setTotalPlanBudget((float) (ObjectUtil.isNotNull(xssfRow.getCell(17).getNumericCellValue())?xssfRow.getCell(17).getNumericCellValue():0));
    	        costBudgetMonths.add(costBudgetMonthTemp);
            }
            
            int flag = 0;
			CostBudgetMonth costBudgetMonthTemp2 = new CostBudgetMonth();
			//将获取的表中数据赋给costBudgetPlan对象中子项目的月度预算.
            if(CollectionUtil.isEmpty(costBudgetPlan.getCostBudgetItems())){
	        	result.setFalse("该预算的所有一级项目为空,请查看");
	        	return result;
            }else{
                for(int i= 0;i<costBudgetPlan.getCostBudgetItems().size();i++){
                	if(CollectionUtil.isEmpty(costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems())){
        	        	result.setFalse("该预算的所有二级项目为空,请查看");
        	        	return result;
                	}else{
                    	for(int j = 0;j<costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().size();j++){
                    		if(ObjectUtil.isNull(costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).getCostBudgetMonth())){
                	        	result.setFalse("该预算有二级项目的无月度预算,请查看");
                	        	return result;
                    		}else{
                    			costBudgetMonthTemp2 = costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).getCostBudgetMonth();
                    			costBudgetMonthTemp2.setJan(costBudgetMonths.get(flag).getJan());
                    			costBudgetMonthTemp2.setFeb(costBudgetMonths.get(flag).getFeb());
                    			costBudgetMonthTemp2.setMar(costBudgetMonths.get(flag).getMar());
                    			costBudgetMonthTemp2.setApr(costBudgetMonths.get(flag).getApr());
                    			costBudgetMonthTemp2.setMay(costBudgetMonths.get(flag).getMay());
                    			costBudgetMonthTemp2.setJun(costBudgetMonths.get(flag).getJun());
                    			costBudgetMonthTemp2.setJul(costBudgetMonths.get(flag).getJul());
                    			costBudgetMonthTemp2.setAug(costBudgetMonths.get(flag).getAug());
                    			costBudgetMonthTemp2.setSep(costBudgetMonths.get(flag).getSep());
                    			costBudgetMonthTemp2.setOct(costBudgetMonths.get(flag).getOct());
                    			costBudgetMonthTemp2.setNov(costBudgetMonths.get(flag).getNov());
                    			costBudgetMonthTemp2.setDece(costBudgetMonths.get(flag).getDece());
                    			costBudgetMonthTemp2.setTotalBudget(costBudgetMonths.get(flag).getTotalBudget());
                    			costBudgetMonthTemp2.setTotalPlanBudget(costBudgetMonths.get(flag).getTotalPlanBudget());
                    			costBudgetPlan.getCostBudgetItems().get(i).getCostBudgetSubItems().get(j).setCostBudgetMonth(costBudgetMonthTemp2) ; 
                    			flag++;
                    		}
                    	}
                	}
                }
            }
            if(flag!=32){
	        	result.setFalse("预算项目总数不为32,与模板不一致,请查看");
	        	return result;
            }
            
	       	 //根据二级项目下的各月预算金额计算各级项目预算金额和预算计划总金额
	       	 result = calculateBudgetYear(costBudgetPlan);
	       	 if(result.isSuccess()){
		        	 costBudgetPlan = 	(CostBudgetPlan) result.getData();
	       	 }else{
	       		 return result;
	       	 }
	       	 
	       	int res = costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
	       	 if(res==0){
	       		 result.setFalse("更新预算计划失败");
			         return result;
	       	 }
	       	 
	       	 //更新子对象
	       	 result = updatePlanAllItems(costBudgetPlan);
	       	 if(result.isSuccess()==false){
	       		 return result;
	       	 }
	       	 
	       	 //计算各级预算月度金额
	       	updateMonth(costBudgetPlan.getId());
       	
            
            
            result.setData(costBudgetPlan);

            return result;
	    	
	    	
	    }

}
